<?php
/**
 * 获取数据字典
 * @param $key      //键值，方便查找数据
 * @param $fileName //字典文件名 目录Common/Dict/
 * @return mixed
 */
function dict($key = '', $fileName = 'Setting') {
    static $_dictFileCache  =   array();
    $file = MODULE_PATH . 'Common' . DS . 'Dict' . DS . $fileName . '.php';
    if (!file_exists($file)){
    	unset($_dictFileCache);
    	return null;
    }
    if(!$key && !empty($_dictFileCache)) return $_dictFileCache;
    if ($key && isset($_dictFileCache[$key])) return $_dictFileCache[$key];
    $data = require_once $file;
    $_dictFileCache = $data;
	return $key ? $data[$key] : $data;
}


//☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆   定时循环方法   ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
function task_get_nextrun($task){
    switch ($task['type']){
        case 'loop':
            $timeOptions = array(
                'day' => $task['day'],
                'hour' => $task['hour'],
                'minute' => $task['minute']
            );
            $lastrun = time();
            $nextrun = task_get_loop_nextrun($timeOptions,$lastrun);
            return array(
                'lastrun' => $lastrun,
                'nextrun' => $nextrun,
                'available' => 1,
            
            );
            
            break;
        case 'once':
        case 'time':
        case 'long':
        default:
            return array(
                'lastrun' => time(),
                'nextrun' => 0,
                'available' => 0,
            
            
            );
    }
}


function task_get_loop_nextrun($timeOptions,$lastrun = 0){
    $day = isset($timeOptions['day']) ? $timeOptions['day'] : 0;
    $hour = isset($timeOptions['hour']) ? $timeOptions['hour'] : 0;
    $minute = isset($timeOptions['minute']) ? $timeOptions['minute'] : 0;
    
    if($lastrun == 0){//首次执行时间
        if($day == 1){//每天执行：day 1 hour 0 minute 0 每天零点执行
            $nextrun = strtotime(date("Y-m-d $hour:$minute:0",time()));
        }else if($day == 0 && $hour == 1){//每小时执行：day 0 hour 1 minute 5 每小时的5分执行
            $nextrun = strtotime(date("Y-m-d H:$minute:0",time()));
        }else if($day == 0 && $hour == 0){
            $nextrun = time() + $minute * 60;
        }else{
            $nextrun = time();
        }
    }else{//下次执行时间
        if($day == 1){//每天执行：day 1 hour 0 minute 0 每天零点执行
            $nextrun = strtotime(date("Y-m-d $hour:$minute:0",time())) + 86400;
        }else if($day == 0 && $hour == 1){//每小时执行：day 0 hour 1 minute 5 每小时的5分执行
            $nextrun = strtotime(date("Y-m-d H:$minute:0",time())) + 3600;
        }else if($day == 0 && $hour == 0){
            $nextrun = time() + $minute * 60;
        }else{
            $nextrun = time();
        }
    }
    
    return $nextrun;
}

function encode_params($params){
    return json_encode($params);
}

function decode_params($params){
    return json_decode($params,true);
}

function Get_Taskk(){
    try {
        $asyntask=M('asyntask');
        $asyntask->startTrans();
        $time=time();
        $task=$asyntask->where(['available'=>1,'nextrun'=>['elt',$time]])->find();
        if(!$task){
            return false;
        }
        $update = task_get_nextrun($task);
        $data=[
            'lastrun'=>$update['lastrun'],
            'nextrun'=>$update['nextrun'],
            'available'=>$update['available']
        ];
        $stmt=$asyntask->where(['id'=>$task['id']])->save($data);
        if(!$stmt){
            return false;
        }
        $asyntask->commit();
        $arr= array(
            'id'=> $task['id'],
            'type'=> $task['type'],
            'name' => $task['name'],
            'cmd'=> $task['cmd'],
            'params'=>decode_params($task['params'])
        );
        throw new Exception($arr);
    } catch(\Exception $ex) {
        $asyntask->rollback();
        return false;
    }
}